<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table>
        <tr>
            <td>姓名:</td>
            <td><input type="text" placeholder="请输入姓名" class="name"></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input type="text" placeholder="请输入年龄" class="age"></td>
        </tr>
        <tr>
            <td>班级:</td>
            <td><input type="text" placeholder="请输入班级编号" class="classId"></td>
        </tr>
    </table>

    <div>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </div>
</body>

</html>

<script src="./config/baseUrl.js"></script>
<script src="./js/jquery-3.6.0.min.js"></script>
<script src="./js/api.js"></script>
<script>
    $(function () {
        let url = location.search
        if (url) {
            let id = url.split('?')[1].split('=')[1]
            console.log(id);
            getByid(id).then(res => {
                $('.name').val(res.data[0].name)
                $('.age').val(res.data[0].age)
                $('.classId').val(res.data[0].classId)
            })
        }
    })

    function save() {
        let url = location.search
        let obj = {
            name: $('.name').val(),
            age: $('.age').val(),
            classId: $('.classId').val()
        }
        if (url) {
            let id = url.split('?')[1].split('=')[1]
            updateStudent(id,obj).then(res=>{
                console.log(res);
            })
        } else {
            addStudent(obj).then(res => {
                console.log(res);
            })
        }
        location.href = 'index.html'
    }
    function cancel() {
        location.href = 'index.html'
    }
</script>
<!-- <script>
    $(function () {
        //当页面加载的时候，需要判断有没有id传入，如果有，则认为是编辑，需要（请求后台）加载数据；如果没有，则认为是新增
        let id = getUrlParam('id');
        console.log(id);
        getProductById(id).then(res => {
            console.log(res);
            let obj = res.data;
            $('[name=productName]').val(obj.productName);
            $('[name=price]').val(obj.price);
            $('[name=stock]').val(obj.stockNum);
            $('[name=supplier]').val(obj.supplier);
        })
    })
    //获取url中的参数
    function getUrlParam(name) {

        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

        var r = window.location.search.substr(1).match(reg); //匹配目标参数

        if (r != null) return unescape(r[2]); return null; //返回参数值

    }
    function save() {
        let id = getUrlParam('id');
        console.log(id);
        let obj = {
            productName: $('[name=productName]').val(),
            price: $('[name=price]').val(),
            stock: $('[name=stock]').val(),
            supplier: $('[name=supplier]').val(),
        }
        // 如果id存在，则表示为编辑，否则为新增
        if (id) {
            updateProduct(id, obj).then(res => {
                console.log(res);
                location.href = './index.html';
            })
        } else {
            let params = {
                productName: obj.productName,
                price: obj.price,
                stockNum: obj.stock,
                supplier: obj.supplier
            }
            addProduct(params).then(res => {
                location.href = './index.html';
            })
        }


    }
    function cancel() {
        location.href = './index.html';
    }
</script> -->